package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_circle;
import cn.com.api.entity.ApiU_circle_post;
import cn.com.api.entity.ApiU_post_reply;
import cn.com.api.entity.ApiU_zan;
import cn.com.api.service.ApiU_circle_postService;
import cn.com.api.service.ApiU_post_replyService;
import cn.com.api.service.CheckService;
import cn.com.util.*;
import net.sf.json.JSONObject;

/**
 * 圈子帖子
 */
@Controller
@RequestMapping(Utils.SYS_API + "circlepost/")
public class ApiU_circle_postController {
	@Autowired
	private ApiU_circle_postService apiU_circle_postService;
	@Autowired
	private ApiU_post_replyService apiU_post_replyService;
	@Autowired
	private CheckService checkService;
	private static Logger logger = Logger.getLogger(ApiU_circle_postController.class);

	/**
	 * 新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertOrUpdateCirclePost")
	public void insertU_circle_post(HttpServletRequest request, HttpServletResponse response,
			ApiU_circle_post apiU_circle_post, String token, String user_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				apiU_circle_post.setAdd_time(DateUtil.nowTimeToString());
				apiU_circle_post.setIs_delete(0);
				if (apiU_circle_post.getId() != null && apiU_circle_post.getId() != 0) {// 修改
					apiU_circle_postService.updateU_circle_post(apiU_circle_post);
				} else {// 新增
					apiU_circle_postService.U_circle_postInsertion(apiU_circle_post);
				}
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getCirclePostInfo")
	public void getCirclePostInfo(HttpServletRequest request, HttpServletResponse response, String token,
			String user_id, String post_id, PageInfo pageInfo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map.put("id", post_id);
				map.put("user_id", user_id);
				ApiU_circle_post u_circle_post = apiU_circle_postService.findU_circle_postsByMap(map);
				Map<String, Object> result = new HashMap<String, Object>();
				result.put("circlepost", u_circle_post);
				map.clear();
				map.put("post_id", post_id);
				List<ApiU_post_reply> apiU_post_replies = apiU_post_replyService.findU_post_replysListPage(map,
						pageInfo);
				result.put("circlepostreplies", apiU_post_replies);
				map.clear();
				map.put("datas", result);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 查询圈子的帖子
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findCirclePostList")
	public void findCirclePostList(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String circle_id, PageInfo pageInfo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map.put("circle_id", circle_id);
				map.put("user_id", user_id);
				List<ApiU_circle_post> list = apiU_circle_postService.findU_circle_postList(map, pageInfo);
				map.clear();
				Map<String, Object> result = new HashMap<String, Object>();
				result.put("postlist", list);
				result.put("pageinfo", pageInfo);
				map.put("datas", result);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteCirclePost")
	public void deleteCirclePost(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String post_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				ApiU_circle_post apiU_circle_post = apiU_circle_postService
						.findU_circle_postById(Integer.valueOf(post_id));
				if (apiU_circle_post.getUser_id().toString().equals(user_id)) {
					apiU_circle_post.setIs_delete(1);
					apiU_circle_postService.updateU_circle_post(apiU_circle_post);
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "删除成功！");
				} else {// 不是本人发的帖子不能删除
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "无权删除帖子！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 回复帖子
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "replyCirclePost")
	public void replyCirclePost(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			ApiU_post_reply apiU_post_reply) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				apiU_post_reply.setAdd_time(DateUtil.nowTimeToString());
				apiU_post_replyService.U_post_replyInsertion(apiU_post_reply);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除帖子的回复内容
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "delCirclePostReply")
	public void delCirclePostReply(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String postReplyId) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				ApiU_post_reply apiU_post_reply = apiU_post_replyService
						.findU_post_replyById(Integer.valueOf(postReplyId));
				if (apiU_post_reply.getUser_id().equals(user_id)) {
					apiU_post_reply.setIs_delete(1);
					apiU_post_replyService.updateU_post_reply(apiU_post_reply);
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "删除成功！");
				} else {// 不是本人发的回复不能删除
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "无权删除回复！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
}
